package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Meeting;
import com.qf.exception.OAException;
import com.qf.mapper.MeetMapper;
import com.qf.service.MeetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class MeetServiceImpl implements MeetService {

    @Resource
    private MeetMapper meetMapper;

    @Override
    public PageInfo<Meeting> findAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Meeting> list = meetMapper.selectAll();
        PageInfo<Meeting> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Meeting> findAll() {
        List<Meeting> meetings = meetMapper.selectAll();
        return meetings;
    }

    @Override
    @Transactional
    public void insertMeet(Meeting meet) {
        int count = meetMapper.insertSelective(meet);
        if (count!=1){
            log.info("【会议室信息】添加会议室失败，meet={}",meet);
            throw new OAException(199,"添加会议室失败");
        }
    }

    @Override
    public Meeting findById(Integer id) {
        Meeting meeting = meetMapper.selectById(id);
        return meeting;
    }

    @Override
    @Transactional
    public void updateCar(Meeting meeting) {
        int count = meetMapper.updateByPrimaryKeySelective(meeting);
        if (count!=1){
            log.info("【会议室信息】修改会议室信息失败，meet={}",meeting);
            throw new OAException(199,"修改会议室信息失败");
        }
    }

    @Override
    @Transactional
    public void delMeet(Integer id) {
        int count = meetMapper.deleteByPrimaryKey(id);
        if (count!=1){
            log.info("【会议室信息】删除会议室信息失败,id={}",id);
            throw new OAException(199,"删除会议室信息失败");
        }
    }

    @Override
    @Transactional
    public void delAllMeet(Integer[] ids) {
        int count=0;
        for (Integer id : ids) {
            meetMapper.deleteByPrimaryKey(id);
            count++;
        }
        if (count!=ids.length){
            log.info("【会议室信息】删除会议室失败，ids={}", Arrays.toString(ids));
            throw new OAException(199,"删除失败");
        }
    }

    @Override
    public  List<Meeting> selectMeet(Integer[] ids) {
        List<Meeting> list=new ArrayList<>();
        for (Integer id : ids) {
            Meeting meet = meetMapper.selectByPrimaryKey(id);
            if (meet==null){
                log.info("【会议室信息】导出失败，ids={}", Arrays.toString(ids));
                throw new OAException(199,"导出会议室信息失败");
            }
            list.add(meet);
        }
        return list;
    }

    @Override
    public PageInfo<Meeting> findByIds(Integer page, Integer size, String[] ids) {

        PageHelper.startPage(page,size);
        //List<Meeting> list=new ArrayList<>();
        List<Meeting> meeting =  meetMapper.selectByIds(ids);
//        for (Integer id : ids) {
//            Meeting meet = meetMapper.selectByPrimaryKey(id);
//            if (meet==null){
//                log.info("【会议室信息】根据id查询失败，ids={}", Arrays.toString(ids));
//                throw new OAException("查询可用会议室信息失败",199);
//            }
//            list.add(meet);
//        }

        return new PageInfo<>(meeting);
    }
}
